//
//  CommunityViewController.m
//  CarLife
//
//  Created by yu on 2019/3/28.
//  Copyright © 2019年 jpas. All rights reserved.
//

#import "CommunityViewController.h"
#import "CommunityCell.h"
#import "CommunityModel.h"
#import "CommunityDetailViewController.h"
//static NSString *kIdentifier = @"communityCellIdentifier";
#define kIdentifier @"communityCellIdentifier"

@interface CommunityViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation CommunityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.dataSource addObjectsFromArray:[self creatModelsWithCount:20]];
    [self.tableView registerNib:NIB_NAMED(@"CommunityCell") forCellReuseIdentifier:kIdentifier];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id cModel = self.dataSource[indexPath.row];
    return [self.tableView cellHeightForIndexPath:indexPath model:cModel keyPath:@"cModel" cellClass:[CommunityCell class] contentViewWidth:[self cellContentViewWith]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommunityCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
    cell.cModel = self.dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommunityModel *cModel = self.dataSource[indexPath.row];
    CommunityDetailViewController *cd = [[CommunityDetailViewController alloc] init];
    cd.cModel = cModel;
    [self.navigationController pushViewController:cd animated:YES];
}

- (CGFloat)cellContentViewWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 适配ios7横屏
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}
- (IBAction)publicArticleAction:(id)sender {
}

- (NSArray *)creatModelsWithCount:(NSInteger)count
{
    NSArray *iconImageNamesArray = @[@"Icon_01",
                                     @"Icon_02",
                                     @"Icon_03",
                                     @"Icon_04",
                                     @"Icon_05",
                                     ];
    
    NSArray *nicknameArray = @[@"唐伯虎",
                            @"风口上的猪",
                            @"Abner",
                            @"坐看云风起",
                            @"得不到的从来矜贵丶"];
    NSArray *commentArray = @[@"1384",
                               @"216",
                               @"4332",
                               @"1",
                               @"55"];
    NSArray *likeArray = @[@"138",
                              @"89",
                              @"400",
                              @"341",
                              @"1"];
    
    NSArray *contentArray = @[@"灯火辉煌的街头，突然袭来了一阵寒流，遥远的温柔，解不了的近愁，是否在随波逐流，夜深人静的时候，我就潜伏在你的伤口，梦是氢气球，向天外飞走，最后都化作乌有一个人在梦游，像奔跑的犀牛，不到最后不罢休，爱若需要厮守，恨更需要自由，爱与恨纠缠不休，我拿什么拯救，当爱覆水难收，谁能把谁保佑，心愿为谁等待，我拿什么拯救，情能见血封喉，谁能把谁保佑，能让爱永不朽",
                           @"夜深人静的时候，我就潜伏在你的伤口，梦是氢气球，向天外飞走，最后都化作乌有",
                           @"一个人在梦游，像奔跑的犀牛",
                           @"爱若需要厮守，恨更需要自由，爱与恨纠缠不休，我拿什么拯救，当爱覆水难收，谁能把谁保佑，心愿为谁等待",
                           @"我拿什么拯救，情能见血封喉，谁能把谁保佑，能让爱永不朽"
                           ];
    
    NSArray *picImageNamesArray = @[ @"img_01",
                                     @"img_02",
                                     @"img_03",
                                     @"img_04",
                                     @"img_05",
                                     @"img_06",
                                     @"img_07",
                                     @"img_08",
                                     @"img_09"
                                     ];
    NSMutableArray *resArr = [NSMutableArray new];
    
    for (int i = 0; i < count; i++) {
        int iconRandomIndex = arc4random_uniform(5);
        int nameRandomIndex = arc4random_uniform(5);
        int contentRandomIndex = arc4random_uniform(5);
        int commentRandomIndex = arc4random_uniform(5);
        int likeRandomIndex = arc4random_uniform(5);
        
        CommunityModel *model = [CommunityModel new];
        model.iconUrl = iconImageNamesArray[iconRandomIndex];
        model.nickname = nicknameArray[nameRandomIndex];
        model.content = contentArray[contentRandomIndex];
        model.likeCount = likeArray[likeRandomIndex];
        model.commentCount = commentArray[commentRandomIndex];
        model.time = @"20分钟前";
        model.browseCount = @"浏览133次";
        
        // 模拟“随机图片”
        int random = arc4random_uniform(9);
        NSMutableArray *temp = [NSMutableArray new];
        for (int i = 0; i < random; i++) {
            int randomIndex = arc4random_uniform(9);
            [temp addObject:picImageNamesArray[randomIndex]];
        }
        if (temp.count) {
            model.picNamesArray = [temp copy];
        }

        [resArr addObject:model];
    }
    return [resArr copy];
}

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setWhiteNavigationBar];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self setDefaultNavigationBar];
}

@end
